<?php
class BlueBarOffer extends Model_Abstract
{
	public function getAllBlueBarOffers() {
		$sql = 'SELECT * FROM bluebar_offer WHERE deleted=0 ORDER BY ord';
		try {
			return $this->_connection->query($sql);
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function getBlueBarOfferById ($id) {
		$sql = 'SELECT * FROM bluebar_offer WHERE id=? AND deleted=0';
		try {
			$reuslt = $this->_connection->query($sql, array($id));
			if (count($reuslt) > 0) {
				return $reuslt[0];
			}
			return null;
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function getBlueBarOfferBySiteId ($siteId) {
		$sql = 'SELECT * FROM bluebar_offer WHERE site_id=? AND deleted=0 AND pause=0 ORDER BY ord';
		try {
			return $this->_connection->query($sql, array($siteId));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function addBlueBarOffer($siteId, $PartnerName, $offerText, $credit) {
		$sql = 'INSERT INTO bluebar_offer SET site_id=?, credit=?, partner_name=?, offer_text=?, update_time=?';
		try {
			return $this->_connection->execute($sql, array($siteId, $credit, $PartnerName, $offerText, time()));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function updateBlueBarOffer($siteId, $PartnerName, $offerText, $credit, $id) {
		$sql = 'UPDATE bluebar_offer SET site_id=?, credit=?, partner_name=?, offer_text=?, update_time=? WHERE id=?';
		try {
			return $this->_connection->execute($sql, array($siteId, $credit, $PartnerName, $offerText, time(), $id));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function deleteBlueBarOffer($id) {
		$sql = 'UPDATE bluebar_offer SET deleted=1 WHERE id=?';
		try {
			return $this->_connection->execute($sql, array($id));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
}